package com.stud.dustteam.dao;

import com.stud.dustteam.model.Product;
import com.stud.dustteam.util.HibernateUtil;
import org.hibernate.classic.Session;

/**
 * Created with IntelliJ IDEA.
 * User: Asus
 * Date: 12.11.12
 * Time: 22:47
 * To change this template use File | Settings | File Templates.
 */
public class ProductDAO implements DAO <Product> {

    @Override
    public void create(Product product) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(product);
        session.getTransaction().commit();
    }

    @Override
    public Product load(Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Product product = (Product) session.load(Product.class, id);
        session.getTransaction().commit();
        return product;
    }

    @Override
    public void delete(Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Product product = (Product) session.load(Product.class, id);
        if(null != product) {
            session.delete(product);
        }
        session.getTransaction().commit();
    }

    @Override
    public void update(Product product) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(product);
        session.getTransaction().commit();
    }
}
